﻿using AgileDevTool.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace AgileDevTool.UserControls
{
    /// <summary>
    /// ExplorerUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ExplorerUserControl : UserControl
    {
        public ExplorerUserControl()
        {
            InitializeComponent();
            UIHelper.tree1 = tree1;
            Loaded+=ExplorerUserControl_Loaded;
        }

        private void ExplorerUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (CacheHelper.CurrentProject==null)
            {
                return;
            }

            var item = new FileItem
            {
                DisplayName=CacheHelper.CurrentProject.Name,
                Name=CacheHelper.CurrentProject.Path,
                Children = new List<FileItem>()
            };
            BuildExplorer(item, CacheHelper.CurrentProject.Path, "\\.git", "\\.vs");

            var items = new List<FileItem>();
            items.Add(item);
            tree1.ItemsSource = items;
        }

        private void BuildExplorer(FileItem item ,string path, params string[] excepts)
        {
            var directories = Directory.GetDirectories(path);
            foreach (var directory in directories)
            {
                if (excepts!=null && excepts.Length>0)
                {
                    if (excepts.Any(a => $"{path}{a}".ToLower() == directory.ToLower()))
                    {
                        continue;
                    }
                }

                var name = Path.GetFileName(directory);
                var child = new FileItem
                {
                    DisplayName= name,
                    Name=directory,
                    Children=new List<FileItem>()
                };
                item.Children.Add(child);

                BuildExplorer(child, directory, excepts);
            }

            var files = Directory.GetFiles(path);
            foreach (var file in files)
            {
                if (excepts!=null && excepts.Length>0)
                {
                    if (excepts.Any(a => $"{path}{a}".ToLower() == file.ToLower()))
                    {
                        continue;
                    }
                }

                var name = Path.GetFileName(file);
                var child = new FileItem
                {
                    DisplayName= name,
                    Name=file,
                    Children=new List<FileItem>()
                };
                item.Children.Add(child);
            }
        }

        private void tree1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var item = (FileItem)e.NewValue;
            if (File.Exists(item.Name))
            {
                UIHelper.tbk_File.Tag = item.Name;
                UIHelper.tbk_File.Text = item.Name;

                var ext_img = ".jpg,.png,.gif".Split(",");
                var ext = Path.GetExtension(item.Name);
                if (ext_img.Any(a => a==ext))
                {
                    UIHelper.img1.Source = new BitmapImage(new Uri(item.Name));
                    UIHelper.img1.Visibility = Visibility.Visible;
                    UIHelper.rtb1.Visibility = Visibility.Collapsed;
                }
                else
                {
                    var content = File.ReadAllText(item.Name, Encoding.UTF8);
                    UIHelper.rtb1.Text = content;
                    UIHelper.rtb1.Visibility = Visibility.Visible;
                    UIHelper.img1.Visibility = Visibility.Collapsed;
                }
            }
        }
    }

    public class FileItem
    {
        public List<FileItem> Children { get; set; }

        public string DisplayName { get; set; }

        public string Name { get; set; }
    }
}
